const capitalizeFirstLetter = str => {
  if (typeof str !== 'string') {
    return ''
  }
  return str.charAt(0).toUpperCase() + str.slice(1)
}

const scssData = {
  '': [`// ---- 基础模块主题配置 ----

// 基础样式
$duxappPrimaryColor: #337ab7;
$duxappSecondaryColor: #5bc0de;
$duxappSuccessColor: #34a853;
$duxappDangerColor: #ea4335;
$duxappWarningColor: #fbbc05;
$duxappPageColor: #fafbf8;
$duxappCustomColor1: #337ab7;
$duxappCustomColor2: #337ab7;
$duxappCustomColor3: #337ab7;

$duxappTextColor1: #373D52;
$duxappTextColor2: #73778E;
$duxappTextColor3: #A1A6B6;
$duxappTextColor4: #FFF;

$duxappTextSize1: 24px;
$duxappTextSize2: 26px;
$duxappTextSize3: 28px;
$duxappTextSize4: 30px;
$duxappTextSize5: 32px;
$duxappTextSize6: 34px;
$duxappTextSize7: 36px;
`],
  common: [`
// 公共属性
$duxappCommonRadius: 16px;`],
  header: [`
// Header组件
$duxappHeaderColor: #fff;
$duxappHeaderTextColor: #000;`],
  button: [`
// Button组件
$duxuiButtonColor: #000;
$duxuiButtonTextColor: #fff;
$duxuiButtonSFontSize: 24px;
$duxuiButtonSPadding: 20px;
$duxuiButtonSHeight: 50px;
$duxuiButtonMFontSize: 26px;
$duxuiButtonMPadding: 25px;
$duxuiButtonMHeight: 56px;
$duxuiButtonLFontSize: 28px;
$duxuiButtonLPadding: 30px;
$duxuiButtonLHeight: 70px;`],
  tabBar: [`
// TabBar组件
$duxuiTabBarNameColor: #666;
$duxuiTabBarNameHoverColor: #000;`],
  tabs: [`
// Tab组件
$duxuiTabLineWidth: 40px;
$duxuiTabLineHeight: 8px;
$duxuiTabLineRadius: 4px;`],
  avatar: [`
// Avatar组件
$duxuiAvatarColor: #{$duxappPrimaryColor};
$duxuiAvatarBgColor: #eee;
$duxuiAvatarIconSize: 56px;
$duxuiAvatarSSize: 64px;
$duxuiAvatarMSize: 84px;
$duxuiAvatarLSize: 120px;`],
  card: [`
// Card组件
$duxuiCardRadius: #{$duxappCommonRadius};
$duxuiCardMargin: 24px;`],

}

module.exports = theme => {
  Object.keys(theme).forEach(key => {
    const value = theme[key]
    if (typeof value !== 'object') {
      scssData[''].push(`$duxapp${capitalizeFirstLetter(key)}: ${value};`)
    } else {
      switch (key) {
        case 'common': {
          if (value.redius) {
            scssData.common.push(`$duxappCommonRedius: ${value}px;`)
          }
          break
        }
        case 'header': {
          if (value.color) {
            scssData.header.push(`$duxappHeaderColor: ${value.color};`)
          }
          if (value.textColor) {
            scssData.header.push(`$duxappHeaderTextColor: ${value.textColor};`)
          }
          break
        }
        case 'button': {
          if (value.color) {
            scssData.button.push(`$duxuiButtonColor: ${value.color};`)
          }
          if (value.textColor) {
            scssData.button.push(`$duxuiButtonTextColor: ${value.textColor};`)
          }
          if (value.sizes) {
            Object.keys(value.sizes).forEach(size => {
              const { h, p, fs } = value.sizes[size]
              scssData.button.push(`$duxuiButton${size.toUpperCase()}FontSize: ${fs}px;`)
              scssData.button.push(`$duxuiButton${size.toUpperCase()}Padding: ${p}px;`)
              scssData.button.push(`$duxuiButton${size.toUpperCase()}Height: ${h}px;`)
            })
          }
          break
        }
        case 'tabBar': {
          if (value.nameColor) {
            scssData.tabBar.push(`$duxuiTabBarNameColor: ${value.nameColor};`)
          }
          if (value.nameHoverColor) {
            scssData.tabBar.push(`$duxuiTabBarNameHoverColor: ${value.nameHoverColor};`)
          }
          break
        }
        case 'tab': {
          if (value.lineWidth) {
            scssData.tabs.push(`$duxuiTabLineWidth: ${value.lineWidth}px;`)
          }
          if (value.lineHeight) {
            scssData.tabs.push(`$duxuiTabLineHeight: ${value.lineHeight}px;`)
          }
          if (value.lineRadius) {
            scssData.tabs.push(`$duxuiTabLineRadius: ${value.lineRadius}px;`)
          }
          break
        }
        case 'avatar': {
          if (value.color) {
            scssData.avatar.push(`$duxuiAvatarColor: ${value.color};`)
          }
          if (value.bgColor) {
            scssData.avatar.push(`$duxuiAvatarBgColor: ${value.bgColor};`)
          }
          if (value.iconSize) {
            scssData.avatar.push(`$duxuiAvatarIconSize: ${value.iconSize}px;`)
          }
          if (value.sizes?.s) {
            scssData.avatar.push(`$duxuiAvatarSSize: ${value.sizes.s};`)
          }
          if (value.sizes?.m) {
            scssData.avatar.push(`$duxuiAvatarMSize: ${value.sizes.m};`)
          }
          if (value.sizes?.l) {
            scssData.avatar.push(`$duxuiAvatarLSize: ${value.sizes.l};`)
          }
          break
        }
        case 'card': {
          if (value.radius !== undefined) {
            scssData.card.push(`$duxuiCardRadius: ${value.radius}px;`)
          }
          break
        }
      }
    }
  })
  return Object.keys(scssData).map(key => {
    scssData[key].splice(1, 0, '\n// 用户样式')
    return scssData[key].join('\n')
  }).join('\n')
}
